package 面试2025.滴滴;

public class 大数相加 {
    /**
     * a = "1", b="9"  result="10"
     *
     * func add(a,b string) string{}
     */
    public static void main(String[] args) {
        String a = "100";
        String b = "9";
        String add = add(a, b);
        System.out.println(add);
    }
    public static String add(String a, String b){

        int i = a.length() - 1, j = b.length() - 1;
        int bit = 0;
        StringBuilder sb = new StringBuilder();
        while(i >= 0 || j >= 0){
            int x = i >= 0 ? a.charAt(i) - '0' : 0;
            int y = j >= 0 ? b.charAt(j) - '0' : 0;
            int sum = x + y + bit;
            sb.append(sum % 10);
            bit = sum / 10;
            i--;
            j--;
        }
        if(bit > 0){
            sb.append(bit);
        }
        return sb.reverse().toString();
    }
}
